package BandB.Tool.GPhoneManager.TaskManager;

import java.util.ArrayList;
import java.util.List;

import BandB.Tool.GPhoneManager.R;
import BandB.Tool.GPhoneManager.TaskManager.Util.SimpleAppInfo;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BlacklistAdapter extends ArrayAdapter<SimpleAppInfo> {

	private static final String TAG = "BlacklistAdapter";
	private final LayoutInflater mInflater;
	private List<SimpleAppInfo> mAppsList;
	private Context mContext;

	public BlacklistAdapter(Context context, ArrayList<SimpleAppInfo> apps) {
		super(context, 0, apps);
		mContext = context;
		mAppsList = apps;
		mInflater = LayoutInflater.from(context);
	}

	public List<SimpleAppInfo> getMAppsList() {
		return mAppsList;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mAppsList.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final SimpleAppInfo info = getItem(position);
		PackageManager pManager = mContext.getPackageManager();
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.blacklist_item, parent,
					false);
		}
		final ImageView iconImage = (ImageView) convertView
				.findViewById(R.id.blacklist_icon);
		try {
			iconImage.setImageDrawable(pManager
					.getApplicationIcon(info.packageName));
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, info.appName + "Application icon is not found.");
			e.printStackTrace();
			iconImage.setImageResource(R.drawable.task_manager);
		}
		final TextView appTextView = (TextView) convertView
				.findViewById(R.id.blacklist_name);
		appTextView.setText(info.appName);
		return convertView;
	}
}
